<?php
/**
 * Created by PhpStorm.
 * User: 式神 (luck48.com)
 * Email: 289650682@qq.com
 * Name: ${NAME}Administrator
 * Date: 2017-03-27
 * Time: 17:05
 */
namespace App\Http\Controllers\Admin;

use App\Http\Models\Article;
use App\Http\Models\ArticleAttr;
use App\Http\Models\Attr;
use App\Http\Models\Category;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;

class ArticleController extends CommonController
{
    //栏目列表
    public function articleList(Request $request){
        if($request->isMethod('post')){
            if($request->get('type')=='cate'){
                $cate=Category::orderBy('listsort','asc')->get();
                $data=[];
                $data['status']=200;
                $obj0=(object) ["name"=>trans('config.all').trans('config.category'),'hover'=>'true','id'=>0,'type'=>0];
                $data['node']=catecontree($cate);
                array_unshift($data['node'],$obj0);
                return $data;
            }else{
                $json=[];
                if($request->get('type')==2){
                    return $json;
                }
                $pagenum=config('system.page_num');
                $p=Input::get('p')?Input::get('p'):1;
                $model=new Article();
                $del=$request->get('del')?$request->get('del'):0;
                $title=$request->get('title')?$request->get('title'):'';
                $list=$model->skip(($p-1)*$pagenum)->take($pagenum)->where('del',$del)->where('title','like','%'.$title.'%')->orderBy('id','desc');
                $count=$model->where('del',$del)->where('title','like','%'.$title.'%');
                $cates=Category::all();
                if($request->get('cid')){
                    $cid=$request->get('cid');
                    $cids=getChildsId($cates,$cid);
                    array_push($cids,$cid);
                    $list=$list->whereIn('cid',$cids)->get()->toArray();
                    $count=$count->whereIn('cid',$cids)->count();
                }else{
                    $count=$count->count();
                    $list=$list->get()->toArray();
                }
                $lists=[];
                foreach($list as $v){
                    $v['catname']=Category::where('id',$v['cid'])->first()->name;
                    array_push($lists,$v);
                }
                $data['list']=$lists;
                $data['title']=trans('config.listtitle');
                $json['status']=200;
                $json['pages']=ceil($count/$pagenum);
                $json['data']=$data;
                return $json;
            }
        }
        $cate=Category::all();//所有分类
        $cates=catlist($cate);//转化成分类树
        return view('admin.article.list',compact('cates'));
    }

    //添加内容
    public function articleAdd(Request $request){
        if($request->isMethod('post')){
            $input=Input::except('_token','attr');
            $input['listsort']=$input['listsort']?$input['listsort']:0;
            DB::beginTransaction();
            $attr=Input::get('attr');
            $input['description']=substr($input['description'], 0, 300);
            try{
                $id=Article::create($input)->id;
                DB::table('article_attr')->where('article_id',$id)->delete();
                if(isset($attr) && count($attr)>0){
                    foreach($attr as $v){
                        DB::table('article_attr')->insert(['article_id'=>$id,'attr_id'=>$v]);
                    }
                }
                DB::commit();
                $data['status']=200;
                $data['msg']=trans('config.submit_success');
                if(isset($input['title'])){
                    $data['url']='reload';
                }
            }catch (\Exception $e) {
                DB::rollBack();
                $data['status']=300;
                $data['msg']=trans('config.submit_error');
            }
            return $data;
        }
        $cate=Category::all();
        $cates=catlist($cate);
        if(!$request->get('cid')){
            $pid=0;
        }else{
            $pid=$request->get('cid');
        }
        $attr=Attr::all();//所有推荐位
        return view('admin.article.add',compact('cates','pid','attr'));
    }

    //编辑栏目
    public function articleEdit(Request $request){
        if($request->isMethod('post')){
            $input=Input::except('_token','id','file','attr');
            DB::beginTransaction();
            $attr=Input::get('attr');
            $input['description']=substr($input['description'], 0, 300);
            try{
                Article::where('id',Input::get('id'))->update($input);
                DB::table('article_attr')->where('article_id',Input::get('id'))->delete();
                if(isset($attr) && count($attr)>0){
                    foreach($attr as $v){
                        DB::table('article_attr')->insert(['article_id'=>Input::get('id'),'attr_id'=>$v]);
                    }
                }
                DB::commit();
                $data['status']=200;
                $data['msg']=trans('config.submit_success');
                if(isset($input['title'])){
                    $data['url']='reload';
                }
            }catch (\Exception $e) {
                DB::rollBack();
                $data['status']=300;
                $data['msg']=trans('config.submit_error');
            }
            return $data;
        }

        $id=Input::get('id')?Input::get('id'):0;
        if($id==0){
            return trans('config.return_error');
        }
        $data=Article::where('id',$id)->first();
        $pid=$data->cid;//当前栏目的上级
        $cate=Category::all();//所有分类
        $cates=catlist($cate);//转化成分类树

        $attr=Attr::all();//所有推荐位
        $attrs=ArticleAttr::where('article_id',$id)->get();
        $attrid=[];
        foreach($attrs as $v){
            array_push($attrid,$v->attr_id);
        }
        return view('admin.article.edit',compact('cates','pid','data','attr','attrid'));
    }

    //删除内容
    public function articleRec(Request $request){
        if($request->isMethod('post')){
            $id=Input::get('id');
            $id=explode(",", $id);
            $m=new Article();
            DB::beginTransaction();
            try{
                $d=[
                    'del'=>1
                ];
                $m->whereIn('id',$id)->update($d);
                DB::commit();
                $data['status']=200;
                $data['msg']=trans('config.submit_success');
                $data['url']='reload';
            }catch (\Exception $e) {
                DB::rollBack();
                $data['status']=300;
                $data['msg']=trans('config.submit_error');
            }
            return $data;
        }
    }
    //删除内容
    public function articleDel(Request $request){
        if($request->isMethod('post')){
            $id=Input::get('id');
            $id=explode(",", $id);
            $m=new Article();
            DB::beginTransaction();
            try{
                $m->whereIn('id',$id)->delete();
                DB::commit();
                $data['status']=200;
                $data['msg']=trans('config.submit_success');
                $data['url']='reload';
            }catch (\Exception $e) {
                DB::rollBack();
                $data['status']=300;
                $data['msg']=trans('config.submit_error');
            }
            return $data;
        }
    }
}